package es.gesfutbol.dao;

import org.hibernate.criterion.Restrictions;
import org.springframework.transaction.annotation.Transactional;

import es.gesfutbol.domini.implementacio.CombinacioGeneral;
import es.gesfutbol.util.HibernateDao;

public class CombinacioGeneralDao extends HibernateDao<CombinacioGeneral, Long> implements ICombinacioGeneralDao{

	private static final String FIXOS = "F";
	private static final String DOBLES = "D";
	private static final String TRIPLES = "T";

	@Override
	@Transactional(readOnly=false)
	public CombinacioGeneral storeIfExist(int numFix, int numDobles, int numTriples) {
		
		CombinacioGeneral combGeneral = (CombinacioGeneral) this.getSessionFactory().getCurrentSession()
							.createCriteria(CombinacioGeneral.class)
							.add(Restrictions.eq("numeroFixos", numFix))
							.add(Restrictions.eq("numeroDobles", numDobles))
							.add(Restrictions.eq("numeroTriples", numTriples))
							.uniqueResult();
		
		if(combGeneral == null){
			CombinacioGeneral cg = new CombinacioGeneral();
			
			String nom = "";
			if(numFix != 0){
				nom = nom + numFix + FIXOS;
			}
			if(numDobles != 0){
				nom = nom + numDobles + DOBLES;
			}
			if(numTriples != 0){
				nom = nom + numTriples + TRIPLES;
			}
			cg.setNom(nom);
			cg.setNumeroFixos(numFix);
			cg.setNumeroDobles(numDobles);
			cg.setNumeroTriples(numTriples);
			this.store(cg);
			combGeneral = cg;
		}
		
		return combGeneral;
	}

}
